import json
from django.http import JsonResponse
from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from rest_framework import viewsets, permissions
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

from apps.blog.models import Collect
from django.core.paginator import Paginator
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status

# Create your views here.
from apps.blog.serializers import CollectSerializer, CollectForm, MessageSerializer


def collect_content(request):
    collect = Collect.objects.all()[0]
    name = collect.name
    type = collect.type
    content = collect.content
    id = collect.id
    publish_data = collect.publish_date
    return_str = 'name: %s, type: %s, ' \
                 'content:%s , id: %s,publish_data: %s ' % (name,
                                                            type,
                                                            content,
                                                            id,
                                                            publish_data)

    return HttpResponse(return_str)

#首页
def get_index_page(request):
    all_collect = Collect.objects.all()
    page = request.GET.get('page')
    if page:
        page = int(page)
    else:
        page = 1
    print('page param:', page)
    all_collect = Collect.objects.all()
    curr_collect = None
    top5_collect_list = Collect.objects.order_by('-publish_date')[:5]
    paginator = Paginator(all_collect, 6)
    page_num = paginator.num_pages
    print('page num:', page_num)
    page_collect_list = paginator.page(page)
    if page_collect_list.has_next():
        next_page = page + 1
    else:
        next_page = page
    if page_collect_list.has_previous():
        previous_page = page - 1

    else:
        previous_page = page

    return render(request, '../templates/blog/index.html',
                  {
                      'collect_list': page_collect_list,
                      'page_num': range(1, page_num + 1),
                      'curr_page': page,
                      'next_page': next_page,
                      'previous_page': previous_page,
                      'top5_collect_list': top5_collect_list,
                      'all_collect': all_collect,

                  }
                  )


#Detial页面
def get_detail_page(request, id):
    all_collect = Collect.objects.all()
    curr_collect = None
    previous_collect = None
    next_collect = None
    for index, collect in enumerate(all_collect):
        if index == 0:
            previous_index = 0
            next_index = index + 1
        elif index == len(all_collect) - 1:
            previous_index = index - 1
            next_index = index
        else:
            previous_index = index - 1
            next_index = index + 1
        if collect.id == id:
            curr_collect = collect
            previous_collect = all_collect[previous_index]
            next_collect = all_collect[next_index]
            break

    section_list = curr_collect.content.split("\n")
    return render(request, '../templates/blog/detail.html',
                  {
                      'all_collect': all_collect,
                      'curr_collect': curr_collect,
                      'section_list': section_list,
                      'previous_collect': previous_collect,
                      'next_collect': next_collect,
                      # 'local': local
                  }
                  )

#List页面
def get_list(request):
    page = request.GET.get('page')
    if page:
        page = int(page)
    else:
        page = 1
    print('page param:', page)
    all_collect = Collect.objects.all()
    top5_collect_list = Collect.objects.order_by('-publish_date')[:5]
    paginator = Paginator(all_collect, 6)
    page_num = paginator.num_pages
    print('page num:', page_num)
    page_collect_list = paginator.page(page)
    if page_collect_list.has_next():
        next_page = page + 1
    else:
        next_page = page
    if page_collect_list.has_previous():
        previous_page = page - 1

    else:
        previous_page = page

    return render(request, '../templates/blog/list.html',
                  {
                      'collect_list': page_collect_list,
                      'page_num': range(1, page_num + 1),
                      'curr_page': page,
                      'next_page': next_page,
                      'previous_page': previous_page,
                      'top5_collect_list': top5_collect_list,

                  }
                  )


#vue
@api_view(["GET"])
def course_list(request):
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    """
    获取所有信息
    """
    if request.method == "GET":
        s = CollectSerializer(instance=Collect.objects.all(),many=True)
        return Response(data=s.data,status=status.HTTP_200_OK)
    # elif request.method == "POST":
    #     s = CollectSerializer(data=request.data)
    #     if s.is_valid():
    #         s.save()
    #         return Response(data=s.data,status=status.HTTP_201_CREATED)
    #     return Response(s.errors,status=status.HTTP_400_BAD_REQUEST)

#echarts
def vision(request):
    all_collect = Collect.objects.all()
    name = list(Collect.objects.values_list('name', flat=True))

    return render(request, '../templates/vision/index.html',
                  {
                      'all_collect': all_collect,
                      'name':name

                  }
                  )

class MessageList(APIView):

    @csrf_exempt
    def get(self,request):
        queryset = Collect.objects.all()
        s = CollectSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class Message(APIView):

    @csrf_exempt
    def get(self):
        queryset = Collect.objects.all()
        s = MessageSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)